<template>
  <common-card
    :title="$t('dashboard.totalUserLogin')"
    :value="this.totalLogin"
  >
    <!-- 底部数据 -->
    <template v-slot:footer>
      <span>{{$t('dashboard.yesterdayUserLogin')}} </span>
      <span class="emphasis">{{preLogin}}</span>
    </template>
  </common-card>
</template>

<script>
import homeview from "@/api/homeview";
import CommonCard from "../CommonCard";
export default {
  name: "index",
  components: {
    CommonCard,
  },
  data() {
    return {
      chartInstance: null,
      totalLogin: 0, //总登录数
      preLogin: 0, //昨日登录
      currentdate: "", //当前时间
      predate: "", //昨天时间
      presevendate: "", //前七天
      xData: [], //x轴数据
      yData: [], //y轴数据
    };
  },
  mounted() {
    this.fetchData();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    //获取前七天时间
    getNowDate() {
      var myDate = new Date() // 获取今天日期
        myDate.setDate(myDate.getDate() - 6)
        var dateArray = []
        var dateTemp
        var flag = 1
        for (var i = 0; i < 7; i++) {
        dateTemp = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate()
        dateArray.push(dateTemp)
        myDate.setDate(myDate.getDate() + flag)
        }
        // console.log( dateArray)
        this.currentdate = dateArray[6]
        this.predate = dateArray[5]
        this.presevendate = dateArray[0]

    },
    fetchData() {
      homeview
        .getUserLogin(this.presevendate)
        .then((res) => {
          // console.log(res);
          this.xData = res.data.result.time;
          this.yData = res.data.result.nums;
          this.updateChart();
        })
        .catch((error) => {
          "error";
        });
    },
    // 更新图表
    updateChart() {
      homeview
        .getUserLogin("2022-09-14")
        .then((res) => {
          //console.log(res);
          this.totalLogin = res.data.result.nums.reduce(function (prev, curr) {
            return prev + curr;
          });
        })
        .catch((error) => {
          "error";
        });
      const size = this.yData.length;
      this.preLogin = this.yData[size - 2];
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
